package com.chen.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.common.VIP;
import com.chen.dao.mapper.CardMapper;
import com.chen.dao.mapper.UserMapper;
import com.chen.dao.pojo.Card;
import com.chen.dao.pojo.User;
import com.chen.service.CardService;
import com.chen.service.UserService;
import com.chen.vo.Result;
import com.chen.vo.param.CardParam;
import java.util.List;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:chenao
 * @DATE:2022/3/18 20:30
 */
@Service
public class CardServiceImpl implements CardService {

    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    private final static String formatPattern="yyyy-MM-dd HH:mm:ss";

    @Override
    public Result listCard(CardParam param) {
        LambdaQueryWrapper<Card> wrapper = new LambdaQueryWrapper<>();
        if (Strings.isNotBlank(param.getCategory())){
            wrapper.like(Card::getCategory,param.getCategory());
        }
        List<Card> cards = cardMapper.selectList(wrapper);
        return Result.success(cards);
    }

    @Override
    public Result buyCard(CardParam param) {
        long month=30*24*60*60*1000l;
        long year =12*30*24*60*60*1000l;
        if (Strings.isBlank(param.getId()) || Strings.isBlank(param.getUserId())){
            return Result.fail(10000,"参数错误");
        }
        Card card = cardMapper.selectById(param.getId());
        User user = userService.queryUserById(param.getUserId());
        Integer balance = user.getBalance();
        Integer overTimes = user.getOverTimes();
        String deadline = user.getDeadline();
        DateTime deadDate;
        if (Strings.isBlank(deadline)){
            deadDate=new DateTime();
        }else{
            deadDate= DateUtil.parse(deadline, formatPattern);
        }
        Integer price = Integer.parseInt(card.getPrice());
        String category = card.getCategory();
        Integer sales = card.getSales();
        sales++;
        card.setSales(sales);
        //先判断余额是否充足，下面就不需要重复判断了
        if (balance<price){
            return Result.fail(10001,"余额不足");
        }
        //代表此次购买的是次卡，以下类似
        if (category.equals(VIP.timesVip)){
            //普通会员才修改其会员类型，升级为次卡会员，其他不管，优先级年卡>月卡>次卡
            if (user.getCategory().equals(VIP.commonVIP)){
                user.setCategory(VIP.timesVip);
            }
            //增加十次约课次数
            overTimes +=10;
            balance=balance-price;
            user.setBalance(balance);
            user.setOverTimes(overTimes);
            userMapper.updateById(user);
            cardMapper.updateById(card);
            return Result.success("购买成功");
        }
        if (category.equals(VIP.monthVIP)){
            if (!user.getCategory().equals(VIP.yearVIP)){
                user.setCategory(VIP.monthVIP);
            }
            long d=deadDate.getTime()+month;
            String format = DateUtil.format(new DateTime(d), formatPattern);
            user.setDeadline(format);
            balance=balance-price;
            user.setBalance(balance);
            userMapper.updateById(user);
            cardMapper.updateById(card);
            return Result.success("购买成功");
        }
        if (category.equals(VIP.yearVIP)){
            user.setCategory(VIP.yearVIP);
            long d=deadDate.getTime()+year;
            String format = DateUtil.format(new DateTime(d), formatPattern);
            user.setDeadline(format);
            balance=balance-price;
            user.setBalance(balance);
            userMapper.updateById(user);
            cardMapper.updateById(card);
        }

        return Result.success("购买成功");
    }
}
